package ruiFight.collect.util;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;


public class JsonUtils {


	private static ObjectMapper mapper = new ObjectMapper();
	
	static{
		mapper.setSerializationInclusion(Include.ALWAYS);
		mapper.configure(Feature.ALLOW_COMMENTS, true);
		mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		java.text.SimpleDateFormat dateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
		mapper.setDateFormat(dateFormat);
	}
	
	public static ObjectMapper getMapper(){
		return mapper;
	}
	
	public static String toJson(Object object) {

		try {
			return mapper.writeValueAsString(object);
		} catch (IOException e) {
			
			return null;
		}
	}
	
	/**
	 * 
	 * @param jsonString
	 * @return
	 */
	public static JsonNode fromJson(String jsonString) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}
		try {
			return mapper.readTree(jsonString);
		} catch (IOException e) {
			
			return null;
		}
	}
	
	
	public static <T> T fromJson(String jsonString, Class<T> clazz) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}

		try {
			return mapper.readValue(jsonString, clazz);
		} catch (IOException e) {
			
			return null;
		}
	}

	/**
	 * 反序列化复杂Collection如List<Bean>, 先使用createCollectionType()或contructMapType(), 然后调用本函数?.
	 * 

	 */
    //* @see #createCollectionType(Class, Class...)
	public static <T> T fromJson(String jsonString, JavaType javaType) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}

		try {
			return (T) mapper.readValue(jsonString, javaType);
		} catch (IOException e) {
			
			return null;
		}
	}
	
	/**
	 * 构建Collection类型.
	 */
	public static JavaType contructCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClass) {
		return mapper.getTypeFactory().constructCollectionType(collectionClass, elementClass);
	}

	/**
	 * 构建Map类型.
	 */
	public static JavaType contructMapType(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) {
		return mapper.getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
	}
	
	/**
	 * 把json转换成不分层次的Map
	 * @param jsonString
	 * @return
	 */
	public static Map<String,Object> removeHierarchy(String jsonString){
		JsonNode root = JsonUtils.fromJson(jsonString);
		return removeHierarchy("",root);
	}
	
	public static Map<String,Object> removeHierarchy(JsonNode root){
		return removeHierarchy("",root);
	}
	
	public static Map<String,Object> removeHierarchy(String parentKey,JsonNode root){
		
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		java.util.Iterator<String> iter = root.fieldNames();
		
		while(iter.hasNext()){
			String fieldName = iter.next();
			JsonNode node = root.get(fieldName);
			
			if(StringUtils.isNotBlank(parentKey)){
				fieldName = parentKey+"/"+fieldName;
			}
			
			if(node.isArray()){
				ArrayNode arrayNode = (ArrayNode)node;
				for(int i=0;i<arrayNode.size();i++){
					map.putAll(removeHierarchy(fieldName+"/"+i,arrayNode.get(i)));
				}
			}else if(node.isObject() || node.isPojo()){
				map.putAll(removeHierarchy(fieldName,node));
			}else{
				map.put(fieldName, node.asText());
			}
		}
		return map;
	}
}
